package com.woniu.aimd.utils;

import org.springframework.ai.document.Document;


import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 *
 */
//@Component
public class TextSplit {
    private final List<Resource>  resources;
    public TextSplit (@Value("classpath:document/*.md") List<Resource> resources) {
        this.resources = resources;
    }
    public List<Document> loadMarkdowns() {

        List <Document> allDocuments = new ArrayList<>();
        resources.forEach(resource -> {
            MarkdownDocumentReaderConfig config = MarkdownDocumentReaderConfig.builder()
                    .withHorizontalRuleCreateDocument(true)
                    .withIncludeCodeBlock(false)
                    .withIncludeBlockquote(false)
                    .withAdditionalMetadata("filename", resource.getFilename())
                    .build();
            // 创建 MarkdownDocumentReader 对象
            MarkdownDocumentReader reader = new MarkdownDocumentReader(resource, config);

            // 获取 Markdown 文档
            allDocuments.addAll(reader.get());
        });

        //遍历资源

        return allDocuments;
    }


}


